package com.entrogy.service.impl;

import com.entrogy.bean.Permission;
import com.entrogy.dao.PermissionMapper;
import com.entrogy.dao.RoleMapper;
import com.entrogy.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName PermissionServiceImpl
 * @Description
 * @Author luoxingjiang
 * @Date 2019/9/2 0002 下午 5:46
 * @Version 1.0
 **/
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<String> listRolePermission(Integer roleId) {
        return permissionMapper.listRolePermission(roleId);
    }

    @Override
    public boolean hasAuthorization(String userId, String permissionCode) {
        List<Integer> roleIds = roleMapper.listRoleId(userId);
        // 用户拥有所有角色的所有权限集合
        List<String> permissionTotalList = new ArrayList<>();
        for (Integer roleId : roleIds) {
            List<String> permissionList = permissionMapper.listRolePermission(roleId);
            permissionTotalList.addAll(permissionList);
        }
        if (permissionTotalList.contains(permissionCode)){
            return true;
        }
        return false;
    }
}
